//
//  UserInfoController.m
//  YYZB
//
//  Created by 顾悦 on 16/3/23.
//
//

#import "UserInfoController.h"

#import "APNsWebService.h"

#import "JPUSHService.h"

@import UIKit;

@implementation UserInfoController

+ (BOOL)isClient {
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"isClient"] ?: NO;
}

+ (void)setIsClient:(BOOL)isClient {
    [[NSUserDefaults standardUserDefaults] setBool:isClient forKey:@"isClient"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (BOOL)hasShownIntro {
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"hasShownIntro"] ?: NO;
}

+ (void)setHasShownIntro:(BOOL)hasShownIntro {
    [[NSUserDefaults standardUserDefaults] setBool:hasShownIntro forKey:@"hasShownIntro"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)userID {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"userID"] ?: @"";
}

+ (void)setUserID:(NSString *)userID {
    [[NSUserDefaults standardUserDefaults] setObject:userID forKey:@"userID"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [UserInfoController registerDeviceToken];
}

+ (NSString *)userNo {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"userNo"] ?: @"";
}

+ (void)setUserNo:(NSString *)userNo {
    [[NSUserDefaults standardUserDefaults] setObject:userNo forKey:@"userNo"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)password {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"password"] ?: @"";
}

+ (void)setPassword:(NSString *)password {
    [[NSUserDefaults standardUserDefaults] setObject:password forKey:@"password"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)sessionKey {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"sessionKey"] ?: @"";
}

+ (void)setSessionKey:(NSString *)sessionKey {
    [[NSUserDefaults standardUserDefaults] setObject:sessionKey forKey:@"sessionKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (BOOL)isLogin {
    return [UserInfoController userID].length && [UserInfoController sessionKey].length;
}

+ (void)clearLoginInfo {
    [UserInfoController setUserID:@""];
    [UserInfoController setSessionKey:@""];
    [UserInfoController setRegisterID:@""];
    [UserInfoController setUserNo:@""];
}

+ (NSString *)defaultCityID {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"defaultCityID"] ?: @"";
}
+ (void)setDefaultCityID:(NSString *)cityID {
    [[NSUserDefaults standardUserDefaults] setObject:cityID forKey:@"defaultCityID"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)defaultCityName {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"defaultCityName"] ?: @"";
}
+ (void)setDefaultCityName:(NSString *)cityName {
    [[NSUserDefaults standardUserDefaults] setObject:cityName forKey:@"defaultCityName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)defaultCommunityID {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"defaultCommunityID"] ?: @"";
}
+ (void)setDefaultCommunityID:(NSString *)communityID {
    [[NSUserDefaults standardUserDefaults] setObject:communityID forKey:@"defaultCommunityID"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)defaultCommunityName {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"defaultCommunityName"] ?: @"";
}
+ (void)setDefaultCommunityName:(NSString *)communityName {
    [[NSUserDefaults standardUserDefaults] setObject:communityName forKey:@"defaultCommunityName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSData *)deviceToken {
    return [[NSUserDefaults standardUserDefaults] dataForKey:@"deviceToken"] ?: nil;
}
+ (void)setDeviceToken:(NSData *)deviceToken {
    [[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:@"deviceToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [UserInfoController registerDeviceToken];
}

+ (NSString *)registerID {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"registerID"] ?: nil;
}
+ (void)setRegisterID:(NSString *)registerID {
    [[NSUserDefaults standardUserDefaults] setObject:registerID forKey:@"registerID"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [UserInfoController registerDeviceToken];
}

+ (NSString *)WeChatShareID {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"WeChatShareID"] ?: nil;
}

+ (void)setWeChatShareID:(NSString *)WeChatShareID {
    [[NSUserDefaults standardUserDefaults] setObject:WeChatShareID forKey:@"WeChatShareID"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)registerDeviceToken {
    NSString *userID = [UserInfoController userID];
    NSString *imie = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSString *registerID = [JPUSHService registrationID];
    NSString *accountNo = [UserInfoController userNo];
    NSString *appType = [UserInfoController isClient] ? @"yiyingzhongbang_client" : @"yiyingzhongbang_service";
    NSString *userType = [UserInfoController isClient] ? @"1" : @"2";
    if (userID.length && imie.length && registerID.length && accountNo.length) {
        [APNsWebService.new registerUserInfoByID:userID imie:imie registerId:registerID accountNo:accountNo appType:appType completionHanlder:^(NSArray *items, NSError *error) {
            if (error) {
                
            } else {
                [APNsWebService.new fetchUserTagByUserType:userType completionHanlder:^(NSArray *items, NSError *error) {
                    if (error) {
                        
                    } else {
                        [JPUSHService setTags:[NSSet setWithArray:items] aliasInbackground:nil];
                    }
                }];
            }
        }];
    }
}

@end
